package com.h3c.nem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.custom.BusinessException;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.annotation.CheckRole;
import com.h3c.nem.dto.TbPersonnelRegisterMiddleDTO;
import com.h3c.nem.dto.beisen.PtEnumDTO;
import com.h3c.nem.enums.RoleType;
import com.h3c.nem.service.ITbPersonnelRegisterMiddleService;
import com.h3c.nem.utils.ReturnCodeData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人事范围和注册地中间表控制层
 *
 * @author chenyifan
 * @since 2022-06-16 09:52:24
 */
@Api(tags = "personnelRegisterMiddle  接口")
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/personnelRegisterMiddle")
public class TbPersonnelRegisterMiddleController {
    /**
     * 服务对象
     */
    private ITbPersonnelRegisterMiddleService personnelRegisterMiddleService;


    /**
     * 列表分页模糊查询
     * @param paramsDTO
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbPersonnelRegisterMiddle", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryFuzzyListForPage(@ModelAttribute TbPersonnelRegisterMiddleDTO paramsDTO,
                                                            @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //分页
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            List<TbPersonnelRegisterMiddleDTO> resultDTOList = personnelRegisterMiddleService.selectFuzzyByDto(paramsDTO);
//            List<PtEnumDTO> personnelRangeEnum = personnelRegisterMiddleService.getPersonnelRangeEnum();
//            Map<String, String> enumMap = personnelRangeEnum.stream().collect(Collectors.toMap(PtEnumDTO::getNameZh, PtEnumDTO::getValue));
//            for (TbPersonnelRegisterMiddleDTO personnelRegisterMiddleDTO : resultDTOList) {
//                personnelRegisterMiddleDTO.setPersonnelCode(enumMap.get(personnelRegisterMiddleDTO.getPersonnelScope()));
//            }
            PageInfo<TbPersonnelRegisterMiddleDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data",pageInfo);
            data.put("msg", "列表分页模糊查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行列表分页模糊查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 查询人事范围枚举
     * @return
     */
    @RequestMapping(value = "/getPersonnelRangeEnum", method = RequestMethod.GET)
    public Result getPersonnelRangeEnum(){
        List<PtEnumDTO> ptEnums = personnelRegisterMiddleService.getPersonnelRangeEnum();
        return new Result(true, 20000, "查询成功", ptEnums);
    }

    /**
     * 数据插入
     * @param paramsDTO
     * @return
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbPersonnelRegisterMiddle", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> save(@RequestBody TbPersonnelRegisterMiddleDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        //执行
        Integer result = personnelRegisterMiddleService.insertDto(paramsDTO);
        data.put("data",result);
        data.put("msg", "数据插入成功");
        data.put("code", ReturnCodeData.SUCCESS);
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 数据修改
     * @param paramsDTO
     * @return
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbPersonnelRegisterMiddle", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> update(@RequestBody TbPersonnelRegisterMiddleDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        //执行
        Integer result = personnelRegisterMiddleService.updateDto(paramsDTO);
        data.put("data",result);
        data.put("msg", "数据修改成功");
        data.put("code", ReturnCodeData.SUCCESS);
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbPersonnelRegisterMiddle", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> delete(@RequestBody TbPersonnelRegisterMiddleDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        //执行
        Integer result = personnelRegisterMiddleService.deleteDto(paramsDTO);
        data.put("data",result);
        data.put("msg", "数据删除成功");
        data.put("code", ReturnCodeData.SUCCESS);
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 人事范围与注册地导入
     * @param file
     * @return
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})
    @ApiOperation(value = "人事范围与注册地导入", notes = "人事范围与注册地导入")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "导入数据文件", required = true, dataType = "MultipartFile", paramType = "body")})
    @RequestMapping(value = "/importPersonnelRegisterMiddle", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> importPersonnelRegisterMiddle(@RequestParam(value="file") MultipartFile file) {
//        Integer result = sdUserService.importUserRoleList(file);
        Integer result = personnelRegisterMiddleService.importUserNew(file);
        JSONObject data = new JSONObject();
        data.put("data", result);
        data.put("msg", "用户角色导入");
        data.put("code", ReturnCodeData.SUCCESS);
        log.info("执行 导入人事范围与注册地完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * 人事范围与注册地导出
     * @param paramsDTO
     * @return
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,
            RoleType.USER_ROLE_OFFER_SSC_MANAGER,
            RoleType.USER_ROLE_DEPT1BP,
            RoleType.USER_ROLE_SS_MANAGER})
    @ApiOperation(value = "人事范围与注册地导出", notes = "人事范围与注册地导出")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbPersonnelRegisterMiddle", paramType = "body")})
    @RequestMapping(value = "/downloadData", method = RequestMethod.POST)
    public Result downloadData(@RequestBody TbPersonnelRegisterMiddleDTO paramsDTO) {
        try {
            File downLoadFile = personnelRegisterMiddleService.download(paramsDTO,"PERSONNEL_REGISTER_MIDDLE_EXPORT","人事范围与注册地导出");
            FileInputStream inputStream = new FileInputStream(downLoadFile);
            byte[] buffer = new byte[(int) downLoadFile.length()];
            inputStream.read(buffer);
            inputStream.close();
            Map<String, String> result = new HashMap<>();
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", downLoadFile.getName());
            return new Result(true, StatusCodeEnum.NORMAL,result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StatusCodeEnum.valueOf(""));
        }
    }
}

